package com.example.bestudentsystem.demos.web.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.bestudentsystem.demos.web.mapper.*;
import com.example.bestudentsystem.demos.web.pojo.Course;
import com.example.bestudentsystem.demos.web.pojo.Inform;
import com.example.bestudentsystem.demos.web.vo.InformVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.sql.Timestamp;
import java.time.Instant;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@RestController
@CrossOrigin
public class informController {
    @Autowired
    InformMapper informMapper;
    @Autowired
    TeacherMapper teacherMapper;
    @Autowired
    StudentMapper studentMapper;
    @Autowired SCMapper scMapper;
    List<String> allSno=new ArrayList<>();

    @PostMapping("/inform")
    public void setAttend(@RequestBody InformVo informVo) {
        Date startDate = Date.from(Instant.parse(informVo.getTime_start()));
        Date endDate = Date.from(Instant.parse(informVo.getTime_end()));
        Timestamp startTimestamp = new Timestamp(startDate.getTime());
        Timestamp endTimestamp = new Timestamp(endDate.getTime());

        Inform inform = new Inform(
                null,
                informVo.getTitle(),
                informVo.getContent(),
                informVo.getCno(),
                startTimestamp,
                endTimestamp
        );
        informMapper.insert(inform);
    }
    @GetMapping("/getInfo1")
    public Inform getInfo1(@RequestParam String sno,@RequestParam String time){
        List<Inform> inform=informMapper.getInform1BySno(sno);
        Date startDate = Date.from(Instant.parse(time));
        Timestamp startTimestamp = new Timestamp(startDate.getTime());
        for (Inform info : inform) {
           if(info.getTime_start().before(startTimestamp)&&info.getTime_end().after(startTimestamp)){
               for (String s:allSno){
                   if(sno.equals(s)){
                       return null;
                   }
               }
               System.out.println("getSuccess");
               return info;
               }
        }
        return null;
    }
    @GetMapping("/getInfo2")
    public IPage<Inform> getInfo2(@RequestParam Integer page,
            @RequestParam Integer size,
            @RequestParam String sno,
               @RequestParam String title){
        Page<Inform> iPage = new Page<>(page,size);
        return informMapper.getInform(iPage,sno,title);
    }

    @GetMapping("/inform")
    public IPage<Inform> getAllInfo(@RequestParam Integer page,
                                  @RequestParam Integer size,
                                  @RequestParam String title){
        Page<Inform> iPage = new Page<>(page,size);
        return informMapper.getInform(iPage,"",title);
    }

    @GetMapping("/signIn")
    public String getSignIn(@RequestParam String sno,@RequestParam String time){
        List<Inform> inform=informMapper.getInform1BySno(sno);
        Date startDate = Date.from(Instant.parse(time));
        Timestamp startTimestamp = new Timestamp(startDate.getTime());
        for (Inform info : inform) {
            if(info.getTime_start().before(startTimestamp)&&info.getTime_end().after(startTimestamp)){
                System.out.println("signInSuccess");
                allSno.add(sno);
                int x=informMapper.getSignInCount(info.getCno());
                float y=scMapper.getAttendance(sno,info.getCno());
                float z=((x-1)*y+1)/x;
                studentMapper.setAttendance(sno,info.getCno(),z);
                return "success";
            }
        }
        System.out.println("signInFail");
        return "fail";
    }
    @PutMapping("/inform")
    public void updateInfo(@RequestBody Inform inform){
        informMapper.updateById(inform);
    }
    @DeleteMapping("/inform/{id}")
    public void deleteInfo(@PathVariable Integer id){
        informMapper.deleteById(id);
    }
}